package lcc.linkshare;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class CouponFeedHandler extends DefaultHandler {

	private List<Offer> offerList = null;
	private Offer offerElement = null;
	private Category categoryElement = null;
	private Promotiontype promotionElement = null;

	private StringBuilder characterValue = new StringBuilder();

	// getter method for Offer List
	public List<Offer> getOfferList() {
		return offerList;
	}

	public CouponFeedHandler()
	{
		offerList = new ArrayList<Offer>();
	}
	public String getOfferId(StringBuilder sb) {
		// String strOffer="";
		String offerId = "";
		int pos = 0;
		if (sb != null) {
			pos = sb.indexOf("offerid=");
		}
		String temp = sb.substring(pos);
		if (temp != null && !"".equals(temp)) {
			offerId = temp.substring(8, temp.indexOf("&"));
		}
		return offerId;
	}

	@Override
	public void startElement(String uri, String localName, String elementName,
			Attributes attributes) throws SAXException {
		try {
			characterValue = new StringBuilder();
			if (elementName.equalsIgnoreCase("link")) {
				offerElement = new Offer();
			}
			if (elementName.equalsIgnoreCase("category")) {
				categoryElement = new Category();
				categoryElement.setCategoryID(attributes.getValue("id"));
			}
			if (elementName.equalsIgnoreCase("promotiontype")) {
				promotionElement = new Promotiontype();
				promotionElement.setPromotiontypeID(attributes.getValue("id"));
			}
			if (elementName.equalsIgnoreCase("network")) {
				offerElement.setNetworkid(attributes.getValue("id"));
			}
		} catch (Exception e) {

		}

	}

	@Override
	public void endElement(String uri, String localName, String elementName)
			throws SAXException {
		try {
			if (elementName.equals("link")) {
				offerList.add(offerElement);
			}
			if (elementName.equals("category")) {
				categoryElement.setCategory(characterValue.toString());
				offerElement.getCategories().add(categoryElement);
			}
			if (elementName.equals("promotiontype")) {
				promotionElement.setPromotiontype(characterValue.toString());
				offerElement.getPromotiontypes().add(promotionElement);
			}
			if (elementName.equals("offerdescription")) {
				offerElement.setOfferdescription(characterValue.toString());
			}
			if (elementName.equals("offerstartdate")) {
				offerElement.setOfferstartdate(characterValue.toString());
			}
			if (elementName.equals("offerenddate")) {
				offerElement.setOfferenddate(characterValue.toString());
			}
			if (elementName.equals("clickurl")) {
				offerElement.setClickurl(characterValue.toString());
				offerElement.setOfferid(getOfferId(characterValue));
			}
			if (elementName.equals("impressionpixel")) {
				offerElement.setImpressionpixel(characterValue.toString());
			}
			if (elementName.equals("advertiserid")) {
				offerElement.setAdvertiserID(characterValue.toString());
			}
			if (elementName.equals("advertisername")) {
				offerElement.setAdvertisername(characterValue.toString());
			}
			if (elementName.equalsIgnoreCase("network")) {
				offerElement.setNetwork(characterValue.toString());
			}
		} catch (Exception e) {

		}
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		characterValue.append(ch, start, length);
		characterValue = new StringBuilder(characterValue.toString()
				.replaceAll("[^\\x00-\\x7f]", ""));

	}
}
